use std::{
    fs::File,
    io::{self, ErrorKind, Read},
};
fn main() {
    println!("============================");
    //test01_call_result();
    println!("============================");
    test02_match_err();
    println!("============================");
    test03_expect_unwarp();
    println!("============================");
    test04_propagating();
    println!("============================");
    test05_err();
    println!("============================");
}
fn test01_call_result() {
    let greeting_file_result = File::open("hello.txt");
    let greeting_file = match greeting_file_result {
        Ok(file) => file,
        Err(error) => panic!("Problem opening the file: {error:?}"),
    };
}
fn test02_match_err() {
    let greeting_file_result = File::open("hello.txt");

    let greeting_file = match greeting_file_result {
        Ok(file) => file,
        Err(error) => match error.kind() {
            ErrorKind::NotFound => match File::create("hello.txt") {
                Ok(fc) => fc,
                Err(e) => panic!("Problem creating the file: {e:?}"),
            },
            _ => {
                panic!("Problem opening the file: {error:?}");
            }
        },
    };
}
fn test03_expect_unwarp() {
    let greeting_file_result = File::open("hello.txt").unwrap();
    let greeting_file_result =
        File::open("hello.txt").expect("hello.txt should be included in this project");
}

fn test04_propagating() {
    let username = match read_username_from_file() {
        Ok(file) => file,
        Err(error) => panic!("Problem opening the file: {error:?}"),
    };

    let username = match read_username_from_file_simplify() {
        Ok(file) => file,
        Err(error) => panic!("Problem opening the file: {error:?}"),
    };

    let username = match read_username_from_file_more_simplify() {
        Ok(file) => file,
        Err(error) => panic!("Problem opening the file: {error:?}"),
    };
    let username = match read_username_from_file_extreme_simplify() {
        Ok(file) => file,
        Err(error) => panic!("Problem opening the file: {error:?}"),
    };
}
fn read_username_from_file() -> Result<String, io::Error> {
    let username_file_result = File::open("hello.txt");

    let mut username_file = match username_file_result {
        Ok(file) => file,
        Err(e) => return Err(e),
    };

    let mut username = String::new();

    match username_file.read_to_string(&mut username) {
        Ok(_) => Ok(username),
        Err(e) => Err(e),
    }
}

fn read_username_from_file_simplify() -> Result<String, io::Error> {
    let mut username_file = File::open("hello.txt")?;
    let mut username = String::new();
    username_file.read_to_string(&mut username)?;
    Ok(username)
}

fn read_username_from_file_more_simplify() -> Result<String, io::Error> {
    let mut username = String::new();
    File::open("hello.txt")?.read_to_string(&mut username)?;
    Ok(username)
}
fn read_username_from_file_extreme_simplify() -> Result<String, io::Error> {
    std::fs::read_to_string("hello.txt")
}

fn test05_err() {
    let greeting_file = File::open("hello.txt")?;
}
//fn main() -> Result<(), Box<dyn Error>> {
//    let greeting_file = File::open("hello.txt")?;
//
//   Ok(())
//}
